package edu.avans.hartigehap.domain;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "WEBORDERITEMS")
public class WebOrderItem implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private WebDish dish;
	private int quantity;

	public WebOrderItem() {

	}

	public WebOrderItem(WebDish dish, int quantity) {
		this.dish = dish;
		this.quantity = quantity;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "WEBORDERITEM_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	// no cascade!!
	@OneToOne()
	@JoinColumn(name="WEBDISH_WEBDISH_ID",referencedColumnName="WEBDISH_ID", updatable=false)
	public WebDish getWebDish() {
		return dish;
	}

	public void setWebDish(WebDish dish) {
		this.dish = dish;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	/* business logic */

	public void incrementQuantity() {
		this.quantity++;
	}

	public void decrementQuantity() {
		assert (quantity > 0);
		this.quantity--;
	}

	@Transient
	public BigDecimal getPrice() {
		BigDecimal b = new BigDecimal(quantity);
		return dish.getTotalPrice().multiply(b);
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof WebOrderItem)) {
			return false;
		}
		WebOrderItem other = (WebOrderItem) object;
		if ((this.id == null && other.id != null)
				|| (this.id != null && !this.id.equals(other.id))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "entity.OrderItem[ id=" + id + " ]";
	}

}
